package wxopen_api

import (
	"context"
	"fmt"
	"net/http"
)

type EmPrivacyKey string

const (
	// 用户信息（微信昵称、头像）
	Key_UserInfo EmPrivacyKey = "UserInfo"
	// 位置信息
	Key_Location EmPrivacyKey = "Location"
	// 地址
	Key_Address EmPrivacyKey = "Address"
	// 发票信息
	Key_Invoice EmPrivacyKey = "Invoice"
	// 微信运动数据
	Key_RunData EmPrivacyKey = "RunData"
	// 麦克风
	Key_Record EmPrivacyKey = "Record"
	// 选中的照片或视频信息
	Key_Album EmPrivacyKey = "Album"
	// 摄像头
	Key_Camera EmPrivacyKey = "Camera"
	// 手机号码
	Key_PhoneNumber EmPrivacyKey = "PhoneNumber"
	// 通讯录（仅写入）权限
	Key_Contact EmPrivacyKey = "Contact"
	// 设备信息
	Key_DeviceInfo EmPrivacyKey = "DeviceInfo"
	// 身份证号码
	Key_EXIDNumber EmPrivacyKey = "EXIDNumber"
	// 订单信息
	Key_EXOrderInfo EmPrivacyKey = "EXOrderInfo"
	// 发布内容
	Key_EXUserPublishContent EmPrivacyKey = "EXUserPublishContent"
	// 所关注账号
	Key_EXUserFollowAcct EmPrivacyKey = "EXUserFollowAcct"
	// 操作日志
	Key_EXUserOpLog EmPrivacyKey = "EXUserOpLog"
	// 相册（仅写入）权限
	Key_AlbumWriteOnly EmPrivacyKey = "AlbumWriteOnly"
	// 车牌号
	Key_LicensePlate EmPrivacyKey = "LicensePlate"
	// 蓝牙
	Key_BlueTooth EmPrivacyKey = "BlueTooth"
	// 日历（仅写入）权限
	Key_CalendarWriteOnly EmPrivacyKey = "CalendarWriteOnly"
	// 邮箱
	Key_Email EmPrivacyKey = "Email"
	// 选中的文件
	Key_MessageFile EmPrivacyKey = "MessageFile"
)

type OwnerSettingInfo struct {
	ContactEmail         string `json:"contact_email,omitempty"`
	ContactPhone         string `json:"contact_phone,omitempty"`
	ContactQQ            string `json:"contact_qq,omitempty"`
	ContactWeixin        string `json:"contact_weixin,omitempty"`
	ExtFileMediaID       string `json:"ext_file_media_id,omitempty"`
	NoticeMethod         string `json:"notice_method"`
	StoreExpireTimestamp string `json:"store_expire_timestamp,omitempty"`
}

type PrivacySettingItem struct {
	PrivacyKey   string `json:"privacy_key"`
	PrivacyText  string `json:"privacy_text"`
	PrivacyLabel string `json:"privacy_label,omitempty"`
}

type PrivacyDescItem struct {
	PrivacyKey  string `json:"privacy_key"`
	PrivacyDesc string `json:"privacy_desc"`
}

type SetPrivacySettingRequest struct {
	PrivacyVer   uint                 `json:"privacy_ver,omitempty"`
	OwnerSetting OwnerSettingInfo     `json:"owner_setting"`
	SettingList  []PrivacySettingItem `json:"setting_list,omitempty"`
}

// SetPrivacySetting 配置小程序用户隐私保护指引
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/privacy_config/set_privacy_setting.html
func (s *WXAService) SetPrivacySetting(ctx context.Context, token string, r *SetPrivacySettingRequest) (*Response, error) {
	u := fmt.Sprintf("cgi-bin/component/setprivacysetting?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodPost, u, r)
	if err != nil {
		return nil, err
	}
	return s.client.Do(ctx, req, nil)
}

type GetPrivacySettingRequest struct {
	PrivacyVer uint `json:"privacy_ver,omitempty"`
}

type GetPrivacySettingResponse struct {
	CodeExist    int                  `json:"code_exist,omitempty"`
	PrivacyList  []string             `json:"privacy_list,omitempty"`
	SettingList  []PrivacySettingItem `json:"setting_list"`
	UpdateTime   int64                `json:"update_time"`
	OwnerSetting OwnerSettingInfo     `json:"owner_setting"`
	PrivacyDesc  struct {
		PrivacyDescList []PrivacyDescItem `json:"privacy_desc_list"`
	} `json:"privacy_desc"`
}

// SetPrivacySetting 配置小程序用户隐私保护指引
// Wechat API docs:
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/privacy_config/get_privacy_setting.html
func (s *WXAService) GetPrivacySetting(ctx context.Context, token string, r *GetPrivacySettingRequest) (*GetPrivacySettingResponse, *Response, error) {
	u := fmt.Sprintf("cgi-bin/component/getprivacysetting?access_token=%v", token)
	req, err := s.client.NewRequest(http.MethodPost, u, r)
	if err != nil {
		return nil, nil, err
	}
	getPrivacySettingResp := new(GetPrivacySettingResponse)
	resp, err := s.client.Do(ctx, req, getPrivacySettingResp)
	if err != nil {
		return nil, resp, err
	}
	return getPrivacySettingResp, resp, nil
}
